package com.mass.core.framework.interceptor;

import javax.servlet.http.HttpSession;  
import javax.servlet.http.HttpSessionEvent;  
import javax.servlet.http.HttpSessionListener;  

import org.springframework.util.ObjectUtils;

import com.mass.core.sys.user.model.SysUser;

import java.util.HashMap; 
/**
 * 
 * @ProjectName:  [demo-ht] 
 * @Package:      [com.mass.core.framework.interceptor.SessionListener.java]  
 * @ClassName:    [SessionListener]   
 * @Description:  [全局变量]   
 * @Author:       [张双喜]   
 * @CreateDate:   [2018年8月23日 上午10:17:40]   
 * @UpdateUser:   [zsx]   
 * @UpdateDate:   [2018年8月23日 上午10:17:40]   
 * @UpdateRemark: [说明本次修改内容] 
 * @Since         [JDK1.7]
 * @Version:      [v1.0]
 *
 */
public class SessionListener implements HttpSessionListener  
{  
      
    public static HashMap<String,HttpSession> sessionMap = new HashMap<String,HttpSession>();  
    /**  
     * 该HashMap以用户名-HttpSession对象存储一个账号只能被一个人登陆的信息。  
     */
    //存用户唯一标识
    public static HashMap<String, String> userMap = new HashMap<String, String>();
    
    //存储用户登录次数
    public static HashMap<String, UserLoginNumber> userLoginNumberMap = new HashMap<String, UserLoginNumber>();
    
    //添加登录次数
    public static synchronized void addUserLoginNumber(String userName)  
    {  
    	UserLoginNumber uNumber = SessionListener.getUserLoginNumberMap().get(userName);
    	if(!ObjectUtils.isEmpty(uNumber)){//登录
    		if(uNumber.getLoginNuber()>5){
    			if((System.currentTimeMillis()-uNumber.getLoginTime())<=1000*60*60){ //一个小时
    				uNumber.setLoginNuber(uNumber.getLoginNuber()+1);
        			uNumber.setLoginTime(System.currentTimeMillis());
        			SessionListener.getUserLoginNumberMap().put(userName, uNumber);
    			}else{
    				UserLoginNumber uln = new UserLoginNumber(1, System.currentTimeMillis());
    	    		SessionListener.getUserLoginNumberMap().put(userName, uln);
    			}
    		}else{
    			if((System.currentTimeMillis()-uNumber.getLoginTime())<=1000*60*60)
    				uNumber.setLoginNuber(uNumber.getLoginNuber()+1);
    			else	
    				uNumber.setLoginTime(System.currentTimeMillis());
    			SessionListener.getUserLoginNumberMap().put(userName, uNumber);
    		}
    	}else{
    		UserLoginNumber uln = new UserLoginNumber(1, System.currentTimeMillis());
    		SessionListener.getUserLoginNumberMap().put(userName, uln);
    	}
    }
    //获取登录次数
    public static synchronized UserLoginNumber getUserLoginNumber(String userName)  
    {  
		return SessionListener.getUserLoginNumberMap().get(userName);
    }
    
    
    public static HashMap<String, String> getUserMap() {
		return userMap;
	}

	public static void setUserMap(HashMap<String, String> userMap) {
		SessionListener.userMap = userMap;
	}

	/**
	 * 
	* @Title: addUser
	* @Description: 添加登录用户  
	* @param userName
	* @param token
	* @throws
	 */
	public static synchronized void addUser(String userName,String token)  
    {  
		SessionListener.getUserMap().put(userName, token);
    }
	/**
	 * 
	* @Title: checkUserName
	* @Description: 验证用户是否登录
	* @param userName
	* @return
	* @throws
	 */
	public static synchronized String getUserToken(String userName)
    {  
		String token = SessionListener.getUserMap().get(userName);
		if(!ObjectUtils.isEmpty(token)){
			return token;
		}else{
			return null;
		}
    }
    public static HashMap<String, UserLoginNumber> getUserLoginNumberMap() {
		return userLoginNumberMap;
	}
	public static void setUserLoginNumberMap(
			HashMap<String, UserLoginNumber> userLoginNumberMap) {
		SessionListener.userLoginNumberMap = userLoginNumberMap;
	}
	public static HashMap<String,HttpSession> getSessionMap(){
    	return sessionMap;
    }
    
      
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {  
    	
    }  
      
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {  
        HttpSession session = httpSessionEvent.getSession();  
        System.out.println("ServletContex初始化");
        delSession(session);  
    } 
    
    public static synchronized void addSession(String key,HttpSession session)  
    {  
    	SessionListener.getSessionMap().put(key, session);
    }  
  
  
    public static synchronized void delSession(HttpSession session)  
    {  
        if(session != null)  
        {  
  
            // 删除单一登录中记录的变量  
            if(session.getAttribute("sysUser") != null)  
            {  
                SysUser user = (SysUser) session.getAttribute("sysUser");  
                SessionListener.getSessionMap().remove(user.getAccount());  
            }  
        }  
    }  
  
    /**
     * 
     * @ProjectName:  [demo-ht] 
     * @Package:      [com.mass.core.framework.interceptor.SessionListener.java]  
     * @ClassName:    [UserLoginNumber]   
     * @Description:  [控制登录次数内部类]   
     * @Author:       [张双喜]   
     * @CreateDate:   [2018年8月28日 下午4:40:32]   
     * @UpdateUser:   [zsx]   
     * @UpdateDate:   [2018年8月28日 下午4:40:32]   
     * @UpdateRemark: [说明本次修改内容] 
     * @Since         [JDK1.7]
     * @Version:      [v1.0]
     *
     */
    public static class UserLoginNumber{
    	
    	private Integer loginNuber;  //登录次数
    	private Long loginTime;   //登录时间
    	
		public UserLoginNumber(Integer loginNuber, Long loginTime) {
			super();
			this.loginNuber = loginNuber;
			this.loginTime = loginTime;
		}
		
		public UserLoginNumber() {
			super();
		}

		public Integer getLoginNuber() {
			return loginNuber;
		}
		public void setLoginNuber(Integer loginNuber) {
			this.loginNuber = loginNuber;
		}
		public Long getLoginTime() {
			return loginTime;
		}
		public void setLoginTime(Long loginTime) {
			this.loginTime = loginTime;
		}	
    	
    }
}  